package com.cadre.common.utils.common;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/* loaded from: UdpGetClientMacAddr.class */
public class UdpGetClientMacAddr {
    private String sRemoteAddr;
    private int iRemotePort = 137;
    private byte[] buffer = new byte[1024];
    private DatagramSocket ds;

    public UdpGetClientMacAddr(String strAddr) throws Exception {
        this.ds = null;
        this.sRemoteAddr = strAddr;
        this.ds = new DatagramSocket();
    }

    public final DatagramPacket send(byte[] bytes) throws IOException {
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(this.sRemoteAddr), this.iRemotePort);
        this.ds.send(dp);
        return dp;
    }

    public final DatagramPacket receive() throws Exception {
        DatagramPacket dp = new DatagramPacket(this.buffer, this.buffer.length);
        this.ds.receive(dp);
        return dp;
    }

    public byte[] GetQueryCmd() throws Exception {
        byte[] t_ns = new byte[50];
        t_ns[0] = 0;
        t_ns[1] = 0;
        t_ns[2] = 0;
        t_ns[3] = 16;
        t_ns[4] = 0;
        t_ns[5] = 1;
        t_ns[6] = 0;
        t_ns[7] = 0;
        t_ns[8] = 0;
        t_ns[9] = 0;
        t_ns[10] = 0;
        t_ns[11] = 0;
        t_ns[12] = 32;
        t_ns[13] = 67;
        t_ns[14] = 75;
        for (int i = 15; i < 45; i++) {
            t_ns[i] = 65;
        }
        t_ns[45] = 0;
        t_ns[46] = 0;
        t_ns[47] = 33;
        t_ns[48] = 0;
        t_ns[49] = 1;
        return t_ns;
    }

    public final String GetMacAddr(byte[] brevdata) throws Exception {
        int i = (brevdata[56] * 18) + 56;
        StringBuffer sb = new StringBuffer(17);
        for (int j = 1; j < 7; j++) {
            String sAddr = Integer.toHexString(255 & brevdata[i + j]);
            if (sAddr.length() < 2) {
                sb.append(0);
            }
            sb.append(sAddr.toUpperCase());
            if (j < 6) {
                sb.append(':');
            }
        }
        return sb.toString();
    }

    public final void close() throws Exception {
        this.ds.close();
    }

    public final String GetRemoteMacAddr() throws Exception {
        send(GetQueryCmd());
        String smac = GetMacAddr(receive().getData());
        close();
        return smac;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(new UdpGetClientMacAddr("192.168.137.111").GetRemoteMacAddr());
    }
}
